<?php
/**
 * $Id$
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

	public $username = '';
	public $uid = '';

	public function __construct()
	{
		parent::__construct();
		$this->username = $this->session->userdata('username');
		$this->uid = $this->session->userdata('uid');
		$this->load->library('form_validation');
		$this->load->model('User_model');
		//加载 ucenter 客户端
		require HERO_ROOT.'/uc_client/client.php';			
	}

	/**
	 * 用户登录 
	 * 
	 * @package User
	 * @category login
	 * @author ybin
	 */
	public function login()
	{
		
		if($this->form_validation->run('signup') === FALSE)
		{
			$this->load->view('cm_header');
			$this->load->view('user_login');
			$this->load->view('cm_footer');
		}
		else
		{
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			$ucresult = uc_user_login($username, $password);
			list($tmp['uid'], $tmp['username'], $tmp['password'], $tmp['email'], $duplicate) = daddslashes($ucresult, 1);
			$ucresult = $tmp;
			if($ucresult['uid'] < 0) 
			{ // 用户不存在(ucenter)
				show_error('用户不存在');
			}

			$member = $this->User_model->get_member_by_uid($ucresult['uid']);
			if(!empty($member))
			{
				$encrypt_password = md5(md5($password).$this->config->item('encryption_key'));
				if($encrypt_password === $member->password)
				{ 
					$this->_make_login_state($member);
				}
				else
				{ // 密码错误
					show_error('密码错误');
				}
			}
			else
			{ // 用户在 ucenter 中存在，hero中不存在
				//show_error('用户不存在');

				$encrypt_password = md5(md5($password).$this->config->item('encryption_key'));
				$result = $this->User_model->regiser_user($ucresult['uid'], $ucresult['username'], $encrypt_password, $ucresult['email']);
				$member = $this->User_model->get_member_by_uid($ucresult['uid']);
				$this->_make_login_state($member);
			}	
		}
		
	}

	/**
	 * 记录登录状态
	 */
	private function _make_login_state($member)
	{
		$sess_data = array(
			'username' => $member->username,
			'uid' => $member->uid
		);
		$this->session->set_userdata($sess_data);

		//同步登陆
		$ucsynlogin = uc_user_synlogin($member->uid);
		$url_forward = site_url('home/index');		
		$message = get_show_message($url_forward, '登陆成功', 1, $ucsynlogin);
		$this->load->view('show_message', array('message' => $message));
	}
	/**
	 * 退出登录
	 *
	 * @package User
	 * @category logout
	 * @author ybin
	 */
    public function logout()
	{
		$this->session->sess_destroy();
		$cookie = array(
			'name' => 'auth',
			'value' => '',
			'expire' => -3600,
			'domain' => '.hero.com',
			'path' => '/',
			'prefix' => 'hero_'
		);
		$this->input->set_cookie($cookie);

		//同步退出
		$ucsynlogout = uc_user_synlogout();	
		$url_forward = site_url('home/index');
		$message = get_show_message($url_forward, '退出成功', 1, $ucsynlogout);
		$this->load->view('show_message', array('message' => $message));

	}	
	
	/**
	 * 新用户注册
	 *
	 * @package User
	 * @category register
	 * @author ybin
	 */
	public function register()
	{
		if($this->form_validation->run('register') === FALSE)
		{
			$this->load->view('cm_header');
			$this->load->view('user_register');
			$this->load->view('cm_footer');
		}
		else
		{
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			$email = $this->input->post('email');
			$uid = uc_user_register($username, $password, $email);
			if($uid > 0) 
			{
				$password = md5(md5($password).$this->config->item('encryption_key'));
				$result = $this->User_model->regiser_user($uid, $username, $password, $email);
				$sess_data = array(
					'username' => $username,
					'uid' => $uid
				);
				$this->session->set_userdata($sess_data);
				redirect('home/index');
			}
			else
			{
				show_error('注册失败, error:'.$uid);
			}
		}
	}

	/**
	 * 修改用户基本信息
	 */
	public function profile()
	{
		$member = $this->User_model->get_member_by_uid($this->uid);
		var_dump($member);
		$viewdata = array(
			'gender' => $member->gender,
			'bday' => $member->bday
		);
		$this->load->view('cm_header', $viewdata);
		$this->load->view('user_profile');
		$this->load->view('cm_footer');
	}
}

/* End of file user.php */
/* Location: ./application/controllers/user.php */


